local console = require 'jass.console'
local runtime = require 'jass.runtime'
local unicode = require "util.unicode"
local japi = require 'jass.japi'
local jass = require 'jass.common'
local storm = require 'jass.storm'
local json = require "util.json"
local urlenc = require "util.url"
local md5 = japi.md5
local post_message = post_message 

local http = "120.55.39.95:3000/error_handle"

if package.local_test then 
    http = "localhost:8080/error_handle"
end 

local error_collect = { }

log = require 'jass.log'

local std_print = print
local log_error = log.error
local log_info  = log.info
local error_handle = runtime.error_handle

-- 重载输出方法
function log.info(...)
	log_info(...)
	console.write(...)
end

function log.error(...)
	local trc = debug.traceback()
	log_error(...)
	log_error(trc)
	std_print(...)
	std_print(trc)
end

function print(...)
	return log.info(...)
end

function runtime.error_handle(message)
    if not error_collect[message] then 
        local traceback = debug.traceback()
        if not package.local_test then 
            message = unicode.a2u(message)
            traceback = unicode.a2u(traceback)
        end 
        local error_md5 = md5( message )
        error_collect[message] = traceback
        -- post_message( "localhost:8080/error_handle", json.encode({
        post_message( http, json.encode({
            error = error_md5,
        }), function (result)
            -- 返回 200 代表 这个错误没有收集过，进行收集
            if result == "200" then 
                -- post_message( "localhost:8080/error_handle", json.encode({
                post_message( http, json.encode({
					author = "merkava",
                    error = error_md5,
                    mapname = "越狱大作战",
                    player = ac.player.self:get_name(),
                    message = urlenc.encode(message),
                    traceback = urlenc.encode(traceback),
                }), function (result)
                    print("---------------------------------------")
                    print("\n")
                    print("错误日志提交成功!")
                    print("\n")
                    print("---------------------------------------")
                end)
            end 
        end)
    end 
    return error_handle(message)
end 

--地图名
local name = japi.GetMapName()
--本地玩家的Id
local pid  = tostring( jass.GetPlayerId( jass.GetLocalPlayer( ) ) + 1 )
--获取时间
local time = os.date( "%H_%M_%S" , os.time( ) )
--获取日期
local day  = os.date( "%Y-%m-%d" , os.time( ) )

local file = name:match('|[Cc]%w%w%w%w%w%w%w%w(%P+)|?[rR]?') or name

if package.local_test then
	log.path = package.local_map_path .. '\\runlog.log'
	storm.save(log.path, "")
else
	log.path = 'logs\\'.. file  .. '\\' .. day .. '\\玩家' .. pid .. '_' .. time .. '.log'
end

--生成日志
log.info( ('-'):rep(0x60) )
log.info( ('[name] - %q'):format(name) )
log.info( ('-'):rep(0x60) )
log.info( ('[version] - %q'):format(package.version) )
log.info( ('-'):rep(0x60) )
log.info( ('[test] - %q'):format(package.local_test) )
log.info( ('-'):rep(0x60) )
log.info( ('[path] - %q'):format(package.local_map_path) )
log.info( ('-'):rep(0x60) )

return log 